package com.sde.controller;

import cn.hutool.core.util.StrUtil;
import com.sde.Constant.ConstantRes;
import com.sde.Enum.ResponseCodeEnum;
import com.sde.exception.BusinessException;
import com.sde.pojo.User;
import com.sde.pojo.dto.UserInfoDto;
import com.sde.service.UserService;
import com.sde.utils.JwtUtils;
import com.sde.utils.Result;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 用户注册
     * @param userName
     * @param password
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestParam String userName,@RequestParam String password){
        log.info("获取到前端传递的要注册的信息：username,{},password，{}",userName,password);
        userService.register(userName,password);
        return Result.success();
    }

    /**
     * 用户登录
     * @param userName
     * @param password
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestParam String userName,@RequestParam String password){
        log.info("前端要登录的用户名：{}",userName);
        User user = userService.login(userName,password);
        // 生成令牌
        Map<String,Object> claims = new HashMap<>();
        claims.put(ConstantRes.USERID,user.getId());
        claims.put(ConstantRes.USERNAME,user.getUsername());
        String token = JwtUtils.generateJwt(claims);
        return Result.success(token);
    }

    /**
     * 获取用户信息
     * @param token
     * @return
     */
    @GetMapping("/userInfo")
    public Result getUserInfo(@RequestHeader(name = "Authorization") String token){
        log.info("获取到token：{}",token);
        //1，判断token是否为空
        if(StrUtil.isEmpty(token)){
            throw new BusinessException(ResponseCodeEnum.NOT_TOKEN);
        }
        //2，解析token获取id
        Claims claims = JwtUtils.parseJWT(token);
        //3，获取用户的id
        Integer userId = Integer.valueOf(claims.get(ConstantRes.USERID).toString());
        //4，判断用户Id是否存在
        if (StrUtil.isEmpty(userId.toString())){
            throw new BusinessException(ResponseCodeEnum.USERID_IS_NOT_EXISTS);
        }
        //5，调用service接口
        User user = userService.getUserInfo(userId);
        return Result.success(user);
    }

    /**
     * 修改用户信息
     * @param userInfoDto
     * @return
     */
    @PutMapping("/update")
    public Result updateUserInfo(@RequestBody UserInfoDto userInfoDto){
        log.info("获取到前端传递过来的参数：{}",userInfoDto);
        userService.updateUserInfo(userInfoDto);
        return Result.success();
    }

    /**
     * 修改用户头像
     * @param avatarUrl
     * @return
     */
    @PatchMapping("/updateAvatar")
    public Result updateAvatar(@RequestParam String avatarUrl){
        log.info("前端传递过来的图像地址：{}",avatarUrl);
        userService.updateAvatar(avatarUrl);
        return Result.success();
    }

    @PatchMapping("/uploadPwd")
    public Result uploadPwd(@RequestBody UserInfoDto userInfoDto){
        log.info("前端调用了修改密码的接口");
        userService.updatePwd(userInfoDto);
        return Result.success();
    }

}
